#include<iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>

const char* g_server_ip = "172.17.30.40";
const int g_server_port = 9600;
const int g_buff_size = 1024;
vector<User> g_user;

class User
{
public:
    string name_;
    string message_;
    User_(string name, string password, int socket)
    {
        this->socket_ = &socket;
        this->name_ = name;
        this->password_ = password;
    }

    sendToFriends_()
    {
        for(int i = 0; i < this->strlen(m_friends); i++)
        {
            send(friends_[i], message_, strlen(message_), 0);
        }
    }

private:
    string password_;
    int* socket_;
    User* friends_;
}

void parseMessage(const char* buff_para, const int buff_len, const int socket_param)
{
   string buff = buff_para;
   if(buff == ("zhangchencan" || "hangkaizhen" || "zhouzhou"))
   {
        g_user.pushback(new User(buff, ));
   }
}

int main()
{
    //create socket
    int server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_fd < 0) 
    {
        std::cerr << "can not create socket\n";
        return 1;
    }

    //bind server ip and server port
    sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = inet_addr(g_server_ip); // receive and ip
    address.sin_port = htons(g_server_port); //host to net

    if (bind(server_fd, (sockaddr*)&address, sizeof(address)) < 0) 
    {
        std::cerr << "bind failed\n";
        return 1;
    }
    while(true)
    {
    	//start listening 
    	if (listen(server_fd, 3) < 0) 
    	{ // most 3 queue waiting
        	std::cerr << "listening failed\n";
        	return 1;
    	}	
    	std::cout << "listening to " << g_server_port << "..." << std::endl;

    	//accept the client connecting
    	sockaddr_in client_addr;
    	socklen_t addr_len = sizeof(client_addr);
    	int client_socket = accept(server_fd, (sockaddr*)&client_addr, &addr_len);

    	if (client_socket < 0) 
    	{
        	std::cerr << "receiving client failed\n";
        	return 1;
    	} 

    	char client_ip[INET_ADDRSTRLEN];
    	inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, INET_ADDRSTRLEN);
    	std::cout << "client ip is : " << client_ip << "port:" << ntohs(client_addr.sin_port) << std::endl;

    	while(true)
	    {
	      //communication
    	  char buffer[g_buff_size] = {0};
     
    
          int bytes_read = recv(client_socket, buffer, g_buff_size, 0);
          if (bytes_read <= 0) 
          {
            std::cout << "client:"<< client_ip << " is offline\n";
	        //close socket 1
    	    close(client_socket);
            close(server_fd);
	        break;
          }
	
          std::cout << "message: " << buffer << std::endl;
	      memset(buffer, 0 , g_buff_size);
	    }
        //const char* response = "消息已接收";
        //send(client_socket, response, strlen(response), 0);
        //memset(buffer, 0, g_buff_size);
    }
    
    //close socket 2
    //close(client_socket);
    //close(server_fd);
    
    return 0;
}
